package cn.cqs.redisitem.controller;

import cn.cqs.redisitem.entity.PcUser;
import cn.cqs.redisitem.service.IPcUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class UserLoginController {
    @Autowired
    IPcUserService userService;

    @PostMapping("/register")
//    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String registerUser(@RequestBody Map map) {
        if (map != null) {
            PcUser u=new PcUser();
            u.setUsername((String) map.get("username"));
            u.setPassword((String) map.get("password"));
            userService.addUser(u);
            return "success";
        }
        return "error";
    }
    @PostMapping("/add")
    public Map<String,String> addLoginUser(@RequestBody Map map){
        Map<String, String> stringMap = new HashMap<>();
        PcUser u;
        if (!map.isEmpty()){
            String username= (String) map.get("username");
            String password = (String) map.get("password");
            if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
                u=new PcUser();
                u.setUsername(username);
                u.setPassword(password);
                userService.addUser(u);
                map.put("code", HttpStatus.OK);
            }
        }
        map.put("code", HttpStatus.EXPECTATION_FAILED);
        return stringMap;
    }
    @PostMapping("/add2")
    public Map<String,String> add(@RequestBody Map map){
        Map<String, String> map2 = new HashMap<>();
//        备注：JSON格式的数据从前端转过来的是LinkHashMap
        LinkedHashMap<String,String> linkedHashMap= (LinkedHashMap<String, String>) map.get("loginUser");
        PcUser u;
        if (linkedHashMap != null ){
            String username = linkedHashMap.get("username");
            String password = linkedHashMap.get("password");
            if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
                u=new PcUser();
                u.setUsername(username);
                u.setPassword(password);
                userService.addUser(u);
                map2.put("message",HttpStatus.OK.getReasonPhrase());
                map2.put("code", String.valueOf(HttpStatus.OK.value()));
                return map2;
            }
        }
        map2.put("code", HttpStatus.EXPECTATION_FAILED.getReasonPhrase());
        return map2;
    }



    @PutMapping( "/update" )
    public Map<String,String> updateLoginUser(@RequestBody Map map){
        Map<String, String> stringMap = new HashMap<>();
        PcUser u;
        if (!map.isEmpty()){
            int id= (int) map.get("id");
            String username= (String) map.get("username");
            String password = (String) map.get("password");
            if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
                u=new PcUser();
                u.setId(id);
                u.setUsername(username);
                u.setPassword(password);
                userService.updateUser(u);
                map.put("code", HttpStatus.OK);
            }
        }
        map.put("code", HttpStatus.EXPECTATION_FAILED);
        return stringMap;
    }

    @DeleteMapping(value = "/deleteById/{id}")
    public String deleteLoginUser(@PathVariable String id) {
     if(StringUtils.isNotEmpty(id)){
//         字符串转Integer 通过Integer.valueof()
         userService.deletUser(id);
         return "删除成功";
     }
        return "删除失败";
    }
    @GetMapping("/findall")
    public Map<String ,Object> findall(){
        Map<String, Object> map = new HashMap<>();
        map.put("loginusers", userService.findAll());
    return map  ;

    }
    @GetMapping("/findbyid/{id}")
    public Map<String,Object> findbyid(@PathVariable String id){
        if(StringUtils.isNotEmpty(id)){
            Map<String,Object> map=new HashMap();
            map.put("loginUsers", userService.findById(id));
            return map;
        }
        return null;
    }
}
